<?php
declare (strict_types=1);

namespace app\business\controller;

use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\response\Json;

class ProductEvaluate extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\ProductEvaluate';

    protected $hasOne = ['product', 'user'];

    public function index()
    {
        return $this->setTable();
    }

    //自动构建table,按需修改
    public function setTable()
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addRow(['field' => 'id', 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'title', 'title' => '评价会员', 'templet' => '<div>{{d.user.account2}}</div>'])
            ->addRow(['field' => 'title', 'title' => '评价商品', 'templet' => '<div>{{d.product.title}}</div>'])
            ->addRow(['field' => 'content', 'title' => '评价内容', 'width' => 500])
            ->addRow(['field' => 'images', 'title' => '评价图片', 'templet' => '#imagesTpl'])
            ->addRow(['field' => 'score', 'title' => '商品评分'])
            ->addRow(['field' => 'create_time', 'title' => '评价时间', 'width' => 150])
            ->addRow(['field' => 'reply_content', 'title' => '回复内容'])
            ->addRow(['field' => 'reply_time', 'title' => '回复时间', 'width' => 150])
            ->addRow(['field' => 'reply_admin_name', 'title' => '回复人'])
            ->addRow(['field' => 'status', 'title' => '状态', 'templet' => '<div>{{d.status == 1?"未回复":"已回复"}}</div>'])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['width' => 1000, 'height' => 1000, 'title' => '回复/查看']])
            ->addTopButtons(['deleteAll'])
            ->setSearch($this->setSearch())
            ->addExtraHtml(['ProductEvaluate'])
            ->addExtraMap(['business_id' => $this->business->id])
            ->fetch();
    }

    //自动构建Search，按需修改
    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['content', '评价内容', 'input', '', '', '', 'like'])
            ->setSearch(['uid', '会员用户名', 'input', '', '\app\common\model\User', 'account,id'])
            ->setSearch(['uid', '商品名称', 'input', '', '\app\common\model\Product', 'title,id'])
            ->setSearch(['status', '回复状态', 'select', [1 => '未回复', 2 => '已回复']]);
    }

    public function edit($name = '', $id = '', $tpl = 'product_evaluate/edit', $info = '')
    {
        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function save($param = [], $model = false): Json
    {
        $param = $this->request->post();
        if (!isset($param['reply_content']) || !$param['reply_content']) {
            return json(['code' => -1, 'msg' => '请填写回复内容']);
        }
        $param['reply_time'] = time();
        $param['reply_admin_id'] = $this->admin->id;
        $param['reply_admin_name'] = $this->admin->admin_name;
        $param['status'] = 2;
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }
}
